package com.lxyk.controller.student;

import com.github.pagehelper.PageInfo;
import com.lxyk.domain.Classes;
import com.lxyk.domain.StudentVO;
import com.lxyk.domain.StudentVO2;
import com.lxyk.domain.SysUser;
import com.lxyk.service.student.CommonService;
import com.lxyk.service.student.TransferService;
import com.lxyk.service.user.ClassesService;
import com.lxyk.service.user.UserService;
import com.lxyk.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

//转班

@Controller
@RequestMapping("/transfer")
public class TransferController {

    @Autowired
    private UserService userService;

    @Autowired
    private CommonService commonService;

    @Autowired
    private TransferService transferService;

    @Autowired
    private ClassesService classesService;

    //查询所有班级转班
    @RequestMapping("findAllTransfer")
    public String findAllTransfer(Model model) {
        //得到当前用户用户名
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
        //根据用户名查询用户信息
        SysUser user = userService.findByName(username);
        //根据用户id查询对应班级
        List<Classes> list = classesService.findByUser(user.getId());
        ArrayList<Integer> counts = new ArrayList<>();
        for (Classes classes : list) {
            Integer id = classes.getId();
            int count = transferService.findCountByCid(id);
            counts.add(count);
        }
        model.addAttribute("counts", counts);
        model.addAttribute("list", list);
        return "transfer_base";
    }

    //查询当前班级转班信息
    @RequestMapping("/transferByCid")
    public String transferByCid(Model model, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO> pageInfo = transferService.transferByCid(cid, pageNum, pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        return "transfer_list";
    }

    //转班申请
    @RequestMapping("/transferApply")
    public String transfer(Model model, Integer id) {
        StudentVO vo = commonService.findStudentById(id);
        List<Classes> list = classesService.findReadClass();
        model.addAttribute("student", vo);
        model.addAttribute("list", list);
        return "student_transfer";
    }

    //转班处理
    @RequestMapping("/transferHandler")
    public String transferHandler(StudentVO vo) {
        //修改学生信息
        transferService.transferHandler(vo);
        //修改班级人数
        classesService.transferClass(vo.getCid(), vo.getOid());
        return "forward:/common/findByCid";
    }

    //根据条件模糊查询转班学生信息
    @RequestMapping("/condition")
    public String findStudentByCondition(Model model, StudentVO vo, Integer cid, @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                         @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        PageUtils.page(pageNum, pageSize);
        PageInfo<StudentVO> pageInfo = transferService.findTransferByCondition(vo, pageNum, pageSize);
        pageInfo.setPageSize(pageSize);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("cid", cid);
        model.addAttribute("name", vo.getName());
        model.addAttribute("transferDate", vo.getTransferDate());
        model.addAttribute("transferClass", vo.getTransferClass());
        return "transfer_list";
    }
}
